<?php
/**
 * Created by PhpStorm.
 * User: teddy
 * Date: 15-12-23
 * Time: 下午2:26
 */

/*判断Request方式，switch到自己需要的方法*/
$request_method = strtolower($_SERVER['REQUEST_METHOD']);

function deleteUser($username)
{
    $mysqli = new mysqli('localhost','root','1','DTXMB');
    if (mysqli_connect_errno())
    {
        echo "{" . '"error"' . ":" . mysqli_connect_error() . "}";
    }
    $sql = "DELETE from Custom WHERE NAME = '" . $username . "'";
    $mysqli->query("set character set 'utf8'");
    $mysqli->query("set names 'utf8'");
    $ret = $mysqli->query($sql);
    $rc = $mysqli->affected_rows;
    return $rc;
}

switch ($request_method) {
    case 'put':
        echo "PUT Method!";
        break;
    case 'post':
        echo "POST Method!";
        break;
    case 'get':
        echo "GET Method!";
        break;
    case 'delete':
        /*request方式为delete，传入json的格式为{"userName":"TeddyLong"}*/
        $raw_post_data = file_get_contents('php://input', 'r');
        $jsonEntity = json_decode($raw_post_data);
        $userName = $jsonEntity->userName;
        echo "You have deleted " . deleteUser($userName) . " record(s)!";
        break;
    default:
        break;
}